<template>
  <div class="history-container">
    <Filter
      v-if="tabList.length > 0"
      :data="tabList"
      @onChanged="onTabChanged"
    ></Filter>
    <div class="list-container" :style="{ height: `${listContainerHeight}px` }">
      <template v-if="listContainerHeight && historyList.length > 0">
        <scroll-view
          v-if="listContainerHeight && historyList.length > 0"
          :scroll-y="true"
          :style="{ height: `${listContainerHeight}px` }"
          :scroll-top="scrollTop"
          @ScrollToLower="onScrollBottom"
        >
          <div
            class="list-item-container"
            v-for="item in historyList"
            :key="item.id"
          >
            <div class="item-row1">
              <div class="item-row1-title">提现</div>
              <div class="item-row1-price">
                + {{ (item.amount / 100).toFixed(2) }} 元
              </div>
            </div>
            <div class="item-row2">
              <div class="item-row2-title">
                <IconFont name="clock" size="14"></IconFont
                ><span style="margin-left: 6px">申请时间</span>
              </div>
              <div class="item-row2-time">{{ item.createTime }}</div>
            </div>
            <div class="item-row3">
              <div class="item-row3-title">
                <IconFont name="success" size="14"></IconFont
                ><span style="margin-left: 6px">申请状态</span>
              </div>
              <div class="item-row3-status" v-if="item.state == 0">已失败</div>
              <div class="item-row3-status" v-if="item.state == 1">提现中</div>
              <div class="item-row3-status" v-if="item.state == 2">已提现</div>
            </div>
          </div>
          <div class="" v-if="loadingFlag" style="height: 20px"></div>

          <Loading
            :page="page"
            :total="last_page"
            :loadingFlag="loadingFlag"
            :tipFlag="tipFlag"
          ></Loading>
        </scroll-view>
        <!-- 空状态 -->
        <template v-if="historyList.length == 0 && !loading">
          <nut-empty description="无数据"></nut-empty>
        </template>
        <!-- 加载状态 -->
        <template v-if="historyList.length == 0 && loading">
          <nut-skeleton
            :style="{ width: '100%', margin: '0.5rem 0.5rem' }"
            width="100%"
            height="15px"
            title
            animated
            row="3"
          >
          </nut-skeleton>
          <nut-skeleton
            :style="{ width: '100%', margin: '0.5rem 0.5rem' }"
            width="100%"
            height="15px"
            title
            animated
            row="3"
          >
          </nut-skeleton>
          <nut-skeleton
            :style="{ width: '100%', margin: '0.5rem 0.5rem' }"
            width="100%"
            height="15px"
            title
            animated
            row="3"
          >
          </nut-skeleton>
          <nut-skeleton
            :style="{ width: '100%', margin: '0.5rem 0.5rem' }"
            width="100%"
            height="15px"
            title
            animated
            row="3"
          >
          </nut-skeleton>
          <nut-skeleton
            :style="{ width: '100%', margin: '0.5rem 0.5rem' }"
            width="100%"
            height="15px"
            title
            animated
            row="3"
          >
          </nut-skeleton>
          <nut-skeleton
            :style="{ width: '100%', margin: '0.5rem 0.5rem' }"
            width="100%"
            height="15px"
            title
            animated
            row="3"
          >
          </nut-skeleton>
          <nut-skeleton
            :style="{ width: '100%', margin: '0.5rem 0.5rem' }"
            width="100%"
            height="15px"
            title
            animated
            row="3"
          >
          </nut-skeleton>
        </template>
      </template>
    </div>
  </div>
</template>
<script setup>
import Filter from "./filter.vue";
import { onMounted, ref } from "vue";
import { IconFont } from "@nutui/icons-vue-taro";
import Loading from "../../components/Loading/index.vue";
const type = ref(2);
const listContainerHeight = ref(null);
import Taro, { useDidShow, usePullDownRefresh } from "@tarojs/taro";
import * as withdrawApi from "../../api/withdraw";
const loading = ref(false);
const page = ref(1);
const size = ref(6);
const state = ref(2);
const loadingFlag = ref(false);
const timer = ref(null);
const last_page = ref(0);
const tipFlag = ref(false);

const scrollTop = ref(0);

const tabList = ref([
  {
    value: 2,
    title: "已提现",
  },
  {
    value: 1,
    title: "提现中",
  },
  {
    value: 0,
    title: "已失败",
  },
]);
const historyList = ref([]);
const onTabChanged = (data) => {
  // console.log(data, "从子组件穿过老的");
  console.log(data, "=====");
  state.value = data.value;
  page.value = 1;
  size.value = 6;
  getlist(page.value, size.value, state.value, 0);
};
useDidShow(() => {
  setTimeout(() => {
    getListContainerHeight();
  }, 600);
});
onMounted(() => {
  loading.value = true;
  getlist(page.value, size.value, state.value, 0);
});
const getlist = async (page, size, state, status) => {
  const res = await withdrawApi.withdrawList({ page, size, state });
  console.log(res);
  if (status) {
    historyList.value = [...historyList.value, ...res.data];
    last_page.value = res.pagination.totalPages;
    loading.value = false;
    loadingFlag.value = false;
    tipFlag.value = true;
  } else {
    historyList.value = [];
    historyList.value = res.data;
    last_page.value = res.pagination.totalPages;
    loading.value = false;
    loadingFlag.value = false;
    tipFlag.value = false;
  }
};
const onScroll = (e) => {
  // console.log(e, "ooooooooooooooo");
  scrollTop.value = e.scrollTop;
};
const onScrollBottom = () => {
  loadingFlag.value = true;
  if (timer.value) {
    clearTimeout(timer.value);
  }
  timer.value = setTimeout(() => {
    if (page.value < last_page.value) {
      page.value++;
      getlist(page.value, size.value, state.value, 1);
    } else {
      loadingFlag.value = false;
      tipFlag.value = true;

      Taro.showToast({
        title: "没有更多数据了呢",
        icon: "none",
      });
    }
  }, 800);
};
const getListContainerHeight = () => {
  const query = Taro.createSelectorQuery()
    .select(".list-container")
    .boundingClientRect();
  query.selectViewport();
  query.exec(function (res) {
    if (res[0]) {
      listContainerHeight.value = res[0].height;
      console.log(res[0].height);
    }
  });
};
</script>
<style lang="scss">
.history-container {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 50px;
  .list-container {
    flex: 1;
    display: flex;
    flex-direction: column;

    padding: 0 25px;
    box-sizing: border-box;
    .list-item-container:last-of-type {
      margin-bottom: 50px;
    }

    .list-item-container {
      border-radius: 15px;
      display: flex;
      flex-direction: column;
      background-color: #fff;
      margin-top: 20px;
      padding: 25px;
      font-size: 24px;
      box-sizing: border-box;
      .item-row1 {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #f6f8f9;
        padding-bottom: 15px;
        .item-row1-title {
          font-size: 28px;
          color: #15181d;
          font-weight: 700;
        }
        .item-row1-price {
          color: #ee0a24;
          font-size: 26px;
        }
      }
      .item-row2 {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 15px;
        color: #858a99;

        .item-row2-title {
          display: flex;
          justify-content: center;
          align-items: center;
          color: #858a99;
        }
      }
      .item-row3 {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 15px;
        color: #858a99;

        .item-row3-title {
          display: flex;
          justify-content: center;
          align-items: center;
          color: #858a99;
        }
      }
    }
  }
}
</style>
